package vn.com.ltv.lo;

import java.util.ArrayList;
import java.util.HashMap;

import vn.com.ltv.lo.common.ConfigsAndroid;
import vn.com.ltv.lo.common.Constants;
import vn.com.ltv.lo.common.SearchObject;
import vn.com.ltv.lo.common.SessionManager;
import vn.com.ltv.lo.daos.CategoryDAO;
import vn.com.ltv.lo.daos.DishDAO;
import vn.com.ltv.lo.daos.SetDAO;
import vn.com.ltv.lo.dtos.CategoryDTO;
import vn.com.ltv.lo.dtos.DishDTO;
import vn.com.ltv.lo.dtos.SetDTO;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

public class Home extends FragmentActivity {

	private ImageView img_left, img_right;
	private LinearLayout menu_left, menu_right;
	private Boolean isSlideRight = false;
	private Boolean isSlideLeft = false;
	private View view_transparent;
	private ViewPager pager;
	private SearchView searchView;
	private ListView listViewSearch;
	ArrayList<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>();
	ArrayList<SearchObject> searchObjects = new ArrayList<SearchObject>();
	
	SessionManager session;
	private String sEmail ="";
	private String sFullName ="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

		init();

		CategoryDAO categoryDAO = new CategoryDAO(Home.this);
		categoryDTOs = categoryDAO.getAll();
		/** Getting fragment manager */
		FragmentManager fm = getSupportFragmentManager();
		pager.setAdapter(new TabsPagerAdapter(fm));

		// ----------------------------------------------------------
	
		
		view_transparent.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isSlideRight) {
					slideOutRightMenu();
				}
				if (isSlideLeft) {
					slideOutLeftMenu();
				}

			}
		});
		// ---------------------------------------------------------

		// ---------------------------------------------------------
		// button show menu left
		img_left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isSlideRight) {
					slideOutRightMenu();
				} else {
					if (isSlideLeft) {
						slideOutLeftMenu();
					} else {
						slideInLeftMenu();
					}
				}

			}
		});

		// button show menu right
		img_right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isSlideLeft) {
					slideOutLeftMenu();
				} else {
					if (isSlideRight) {
						slideOutRightMenu();
					} else {
						slideInRightMenu();
					}
				}

			}
		});
		
		searchView.onActionViewExpanded();
		
		//search key
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			
			@Override
			public boolean onQueryTextSubmit(String query) {
				Log.d("tu khoa search", query);
				searchObjects.clear();
				SetDAO setDAO = new SetDAO(Home.this);
				ArrayList<SetDTO> setDTOs = setDAO.getAllByCategoryByName(query);
				Log.d("danh sach set ", setDTOs.size()+" mon");
				if (setDTOs.size() > 0) {
					for (int i = 0; i < setDTOs.size(); i++) {
						SearchObject searchObject = new SearchObject(setDTOs.get(i).getSetId(), 
								setDTOs.get(i).getSetNm(),
								""	, false, setDTOs.get(i).getSetRtAvrg());
						searchObjects.add(searchObject);
					}
				}
				
				DishDAO dishDAO = new DishDAO(Home.this);
				ArrayList<DishDTO> dishDTOs = dishDAO.getAllByCategoryByName(query);
				Log.d("danh sach dish ", dishDTOs.size()+" mon");
				if (dishDTOs.size() > 0) {
					for (int i = 0; i < dishDTOs.size(); i++) {
						SearchObject searchObject = new SearchObject(dishDTOs.get(i).getDshId(), 
								dishDTOs.get(i).getDshNm(),
								""	, true, dishDTOs.get(i).getDshRtAvrg());
						searchObjects.add(searchObject);
					}
				}
				
				
				
			listViewSearch.setAdapter(new SearchAdapter(Home.this, searchObjects));
				return true;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		
		
	}
	
	private void init(){
		img_left = (ImageView) findViewById(R.id.img_left);
		img_right = (ImageView) findViewById(R.id.img_right);
		menu_left = (LinearLayout) findViewById(R.id.menu_left);
		menu_right = (LinearLayout) findViewById(R.id.menu_right);
		view_transparent = findViewById(R.id.view_transparent);
		pager = (ViewPager) findViewById(R.id.pager);
		menu_left.setVisibility(View.GONE);
		menu_right.setVisibility(View.GONE);
		searchView = (SearchView) findViewById(R.id.searchView1);
		listViewSearch = (ListView) findViewById(R.id.listview_search);
		
		session = new SessionManager(getApplicationContext());
		// get user data from session		
        HashMap<String, String> user = session.getUserDetails();         
        sFullName = user.get(SessionManager.KEY_NAME);         
        sEmail = user.get(SessionManager.KEY_EMAIL);
	}

	// show menu right
	private void slideInRightMenu() {
		Animation animation = ConfigsAndroid.isFromRightAnimation(300);
		menu_right.setVisibility(View.VISIBLE);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				isSlideRight = true;
				view_transparent.setVisibility(View.VISIBLE);
			}
		});
		menu_right.startAnimation(animation);
	}

	// hide menu right
	private void slideOutRightMenu() {
		Animation animation = ConfigsAndroid.outToRightAnimation(300);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				isSlideRight = false;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				menu_right.setVisibility(View.GONE);
				view_transparent.setVisibility(View.GONE);
				menu_right.clearAnimation();
			}
		});
		menu_right.startAnimation(animation);

	}

	// show menu right
	private void slideInLeftMenu() {
		Animation animation = ConfigsAndroid.inFromLeftAnimation(300);
		menu_left.setVisibility(View.VISIBLE);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				isSlideLeft = true;
				view_transparent.setVisibility(View.VISIBLE);
			}
		});
		menu_left.startAnimation(animation);
	}

	// hide menu right
	private void slideOutLeftMenu() {
		Animation animation = ConfigsAndroid.outToLeftAnimation(300);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				isSlideLeft = false;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				menu_left.setVisibility(View.GONE);
				view_transparent.setVisibility(View.GONE);
				menu_left.clearAnimation();
			}
		});
		menu_left.startAnimation(animation);

	}
	
	public class SearchAdapter extends BaseAdapter{

		Context context;
		ArrayList<SearchObject> categories = new ArrayList<SearchObject>();
		LayoutInflater infalInflater;
		FoodHolder catListHolder;
		
		public SearchAdapter(Context context, ArrayList<SearchObject> categories){
			this.context = context;
			this.categories = categories;
			infalInflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return categories.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			TextView textView = null;
			
			if (convertView == null) {
				catListHolder = new FoodHolder();
				convertView = infalInflater.inflate(
						R.layout.item_listview_search, null);
				textView = (TextView) convertView
						.findViewById(R.id.item_text_search);
				  
				catListHolder.textViewName = textView;
				convertView.setTag(catListHolder);

			} else {
	  			catListHolder = (FoodHolder) convertView.getTag();
				textView = catListHolder.textViewName;
				
			}
			
			textView.setText(categories.get(position).getName());
			
			convertView.setOnClickListener(new View.OnClickListener() {
				int p = position;
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Home.this, DetailActivity.class);
					intent.putExtra(Constants.EXTRA_ID, categories.get(p).get_id());
					
					if (categories.get(p).getIsType()) {
						intent.putExtra(Constants.EXTRA_TYPE, Constants.EXTRA_VALUE_DISH);
					}else{
						intent.putExtra(Constants.EXTRA_TYPE, Constants.EXTRA_VALUE_SET);
					}
					startActivity(intent);
				}
			});
			return convertView;
		}
		
		public class FoodHolder {
			TextView textViewName;
			
		}
	}

	public class TabsPagerAdapter extends FragmentPagerAdapter {

		public TabsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			
			
			if (position == 2) {
				Constants.isType = true;
				
			}else{
				Constants.isType = false;
				
			}
			
			return LogoFragment.newInstance(Integer.parseInt(categoryDTOs.get(position).getCtgId()));
		}

		@Override
		public int getCount() {

			return categoryDTOs.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return categoryDTOs.get(position).getCtgNm() +"";
		}

	}
}
